package com.jalonjia.paste.service.impl;

import com.jalonjia.paste.pojo.Paste;
import com.jalonjia.paste.service.PasteService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author jalon
 */
@Service("redisPasteService")
@ConditionalOnProperty(value = "paste.datasource", havingValue = "redis")
public class RedisPasteServiceImpl implements PasteService {
    @Resource
    RedisTemplate<String, Paste> redisTemplate;
    private static final String PREFIX = "paste:";

    @Override
    public void add(Paste paste) {
        redisTemplate.opsForValue().set(
                PREFIX + paste.getId(),
                paste,
                paste.getDelay(TimeUnit.MILLISECONDS),
                TimeUnit.MILLISECONDS);
    }

    @Override
    public Paste getById(String key) {
        return redisTemplate.opsForValue().get(PREFIX + key);
    }

}
